<template>
  <div class="app-container">
    <el-input v-model="search" placeholder="输入编号或者名称关键字搜索" size="mini" align="right"/>
    <el-table
      v-loading="Loading"
      :data="data.filter(data => !search || data.Name.toLowerCase().includes(search.toLowerCase()) || data.No.toLowerCase().includes(search.toLowerCase()))"
      element-loading-text="Loading"
      style="width: 100%"
    >
      <el-table-column type="expand" class-name="column-background">
        <template slot-scope="props">
          <el-form label-position="left" inline class="table-expand" >
            <el-form-item label="安全存量">
              <span>{{ props.row.MerchandiseSafetyStock }}</span>
            </el-form-item>
            <el-form-item label="单个进价(元)">
              <span>{{ props.row.PurchasePrice }}</span>
            </el-form-item>
            <el-form-item label="单个重量(g)">
              <span>{{ props.row.Weight }}</span>
            </el-form-item>
            <el-form-item label="件/毛重(kg)">
              <span>{{ props.row.PackgeWeight }}</span>
            </el-form-item>
            <el-form-item label="装箱数">
              <span>{{ props.row.PackgeNum }}</span>
            </el-form-item>
            <el-form-item label="整件箱规">
              <span>{{ props.row.PackgeCube }}</span>
            </el-form-item>
            <el-form-item label="供应商">
              <span>{{ props.row.Supply }}</span>
            </el-form-item>
            <el-form-item label="位置">
              <span>{{ props.row.Location }}</span>
            </el-form-item>
            <el-form-item label="原装标签">
              <span>{{ props.row.Logo }}</span>
            </el-form-item>
            <el-form-item label="备注">
              <span>{{ props.row.Remark }}</span>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="编号" prop="No"/>
      <el-table-column label="名称" prop="Name"/>
      <el-table-column label="库存" prop="Number"/>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="info" icon="el-icon-edit" circle @click="handleEdit(scope.$index, scope.row)"/>
          <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="handleDelete(scope.$index, scope.row)"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 跟新表单 -->
    <el-dialog :visible.sync="dialogFormVisible" title="修改配件信息" width="99%">
      <el-form v-enterToNext="true" ref="postForm" :model="postForm" :rules="rules" status-icon label-width="150px" >
        <el-form-item label="编号" prop="No">
          <el-input v-model="postForm.No"/>
        </el-form-item>
        <el-form-item label="名称" prop="Name" width="29%">
          <el-input v-model="postForm.Name"/>
        </el-form-item>
        <el-form-item label="安全存量" prop="MerchandiseSafetyStock">
          <el-input v-model.number="postForm.MerchandiseSafetyStock"/>
        </el-form-item>
        <el-form-item label="单个进价(元)" prop="PurchasePrice">
          <el-input v-model.number="postForm.PurchasePrice"/>
        </el-form-item>
        <el-form-item label="单个重量(g)" prop="Weight">
          <el-input v-model.number="postForm.Weight"/>
        </el-form-item>
        <el-form-item label="件/毛重(kg)" prop="PackgeWeight">
          <el-input v-model.number="postForm.PackgeWeight"/>
        </el-form-item>
        <el-form-item label="装箱数" prop="PackgeNum">
          <el-input v-model.number="postForm.PackgeNum"/>
        </el-form-item>
        <el-form-item label="整件箱规" prop="PackgeCube">
          <el-input v-model="postForm.PackgeCube"/>
        </el-form-item>
        <el-form-item label="供应商" prop="Supply">
          <el-select v-model="postForm.Supply" filterable clearable placeholder="请选择" size="mini">
            <el-option
              v-for="item in supplys"
              :key="item.ID"
              :label="item.Name"
              :value="item.ID"/>
          </el-select>
        </el-form-item>
        <el-form-item label="位置" prop="Location">
          <el-autocomplete
            v-model="postForm.Location"
            :fetch-suggestions="querySearch"
            class="inline-input"
            placeholder="请输入位置"
            @select="handleSelect"
          />
        </el-form-item>
        <el-form-item label="备注" prop="Remark">
          <el-input v-model="postForm.Remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.column-background {
  background-color: gainsboro;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 150px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import { getComponentsAsAdmin, deleteComponent, updateComponent } from '@/api/component'
import { getSupply } from '@/api/supply'
import { getLocation } from '@/api/commodity'

export default {
  data() {
    return {
      Loading: true,
      data: [],
      supplys: [],
      locations: [],
      search: '',
      dialogFormVisible: false,
      postForm: {
        ID: '',
        No: '',
        Name: '',
        Remark: '',
        Supply: '',
        Location: '',
        MerchandiseSafetyStock: 0,
        PackgeNum: 0,
        PackgeCube: '',
        PackgeWeight: 0,
        Weight: 0,
        PurchasePrice: 0
      },
      rules: {
        Name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        PackgeNum: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        Supply: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        Location: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        MerchandiseSafetyStock: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.Loading = true
      getComponentsAsAdmin().then(response => {
        console.log(response)
        this.data = response
        this.Loading = false
      })
      getSupply().then(response => {
        console.log(response)
        this.supplys = response.map(item => {
          return {
            ID: item.Name,
            Name: item.Name
          }
        })
      })
      getLocation().then(response => {
        console.log(response)
        this.locations = response.map(item => {
          return {
            value: item.Location
          }
        })
      })
    },
    handleDelete(index, data) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.Loading = true
          console.log(data.ID)
          deleteComponent(data.ID)
            .then(response => {
              console.log(response)
              this.$message.success('删除成功')
              this.data.splice(index, 1)
              this.Loading = false
            })
            .catch(() => {
              this.Loading = false
              this.$message.error('删除失败')
            })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    handleEdit(index, data) {
      this.dialogFormVisible = true
      this.postForm.ID = data.ID
      this.postForm.No = data.No
      this.postForm.Name = data.Name
      this.postForm.MerchandiseSafetyStock = data.MerchandiseSafetyStock
      this.postForm.Remark = data.Remark
      this.postForm.PackgeNum = data.PackgeNum
      this.postForm.PackgeCube = data.PackgeCube
      this.postForm.PackgeWeight = data.PackgeWeight
      this.postForm.Weight = data.Weight
      this.postForm.Supply = data.Supply
      this.postForm.Location = data.Location
      this.postForm.PurchasePrice = data.PurchasePrice
    },
    handleEditConfirm() {
      this.Loading = true
      updateComponent(this.postForm.ID, this.postForm)
        .then(response => {
          console.log(response)
          this.$message.success('更新成功')
          this.Loading = false
          this.dialogFormVisible = false
          this.fetchData()
        })
        .catch(() => {
          this.Loading = false
          this.$message.error('更新失败')
        })
    },
    querySearch(queryString, cb) {
      var locations = this.locations
      var results = queryString ? locations.filter(this.createFilter(queryString)) : locations
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    }
  }
}
</script>
